package com.mbyte.easy.myVacation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.myVacation.entity.TVacation;
import com.mbyte.easy.myVacation.service.ITVacationService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.studentInfo.entity.TStudent;
import com.mbyte.easy.studentInfo.service.ITStudentService;
import com.mbyte.easy.util.PageInfo;
import com.mbyte.easy.util.Utility;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;

import java.time.Duration;
import java.util.List;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 * @author lp
 * @since 2019-03-11
 */
@Controller
@RequestMapping("/myVacation/tVacation")
public class TVacationController extends BaseController  {

    private String prefix = "myVacation/tVacation/";

    @Autowired
    private ITVacationService tVacationService;
    @Autowired
    private ITStudentService studentService;

    /**
     * 查询列表
     *
     * @param model
     * @param pageNo
     * @param pageSize
     * @param tVacation
     * @return
     */
    @RequestMapping
    public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String startTimeSpace, String endTimeSpace, String createTimeSpace, String deleteTimeSpace, TVacation tVacation) {
        Page<TVacation> page = new Page<TVacation>(pageNo, pageSize);
        QueryWrapper<TVacation> queryWrapper = new QueryWrapper<TVacation>();
        SysUser currentUser = Utility.getCurrentUser();
        queryWrapper = queryWrapper.like("user_id",currentUser.getId());
        if(!ObjectUtils.isEmpty(tVacation.getVacationStatus())) {
            queryWrapper = queryWrapper.like("vacationStatus",tVacation.getVacationStatus());
        }
        if(!ObjectUtils.isEmpty(tVacation.getVerifyStatus())) {
            queryWrapper = queryWrapper.like("verifyStatus",tVacation.getVerifyStatus());
        }
        if(!ObjectUtils.isEmpty(tVacation.getIsVerified())) {
            queryWrapper = queryWrapper.like("isVerified",tVacation.getIsVerified());
        }
        IPage<TVacation> pageInfo = tVacationService.page(page, queryWrapper);
        model.addAttribute("startTimeSpace", startTimeSpace);
        model.addAttribute("endTimeSpace", endTimeSpace);
        model.addAttribute("deleteTimeSpace", deleteTimeSpace);
        model.addAttribute("searchInfo", tVacation);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"list";
    }

    /**
     * 添加跳转页面
     * @return
     */
    @GetMapping("addBefore")
    public String addBefore(Model model){
        SysUser currentUser = Utility.getCurrentUser();
        model.addAttribute("currentUser",currentUser);
        return prefix+"add";
    }
    /**
     * 添加
     * @param tVacation
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(TVacation tVacation){
        if (tVacation.getStartTime().isEqual(tVacation.getEndTime())){
            return error("时间输入错误！假期开始时间不可与结束时间相同！");
        }else if (tVacation.getStartTime().isAfter(tVacation.getEndTime())){
            return error("时间输入错误！假期开始时间不可在结束时间之后！");
        }else if (Duration.between(tVacation.getStartTime(),LocalDateTime.now()).toMinutes() >2){
            return error("时间输入错误！假期开始时间不可在当前时间之前！");
        }
        TStudent tStudent = studentService.selectByUserId(tVacation.getUserId());
        System.err.println(tStudent);
        tVacation.setIsVerified(0);
        tVacation.setVacationStatus(0);
        tVacation.setVerifyStatus(0);
        tVacation.setDeleteStatus(0);
        tVacation.setMyStatus(tStudent.getSex());
        System.err.println(tVacation);
        return toAjax(tVacationService.insert(tVacation));
    }
    /**
     * 添加跳转页面
     * @return
     */
    @GetMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id){
        model.addAttribute("tVacation",tVacationService.getById(id));
        return prefix+"edit";
    }
    /**
     * 添加
     * @param tVacation
     * @return
     */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(TVacation tVacation){
        return toAjax(tVacationService.updateById(tVacation));
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(tVacationService.removeById(id));
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("deleteAll")
    @ResponseBody
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(tVacationService.removeByIds(ids));
    }

}

